﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Utils;
using DevExpress.Web.ASPxEditors;
using System.Web.Script.Services;
using System.Web.Services;

public partial class PromotionInfo_ContractManagement : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "4002"; }
    }

    protected override string UserFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return "IUDQ";
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? CMSUser.GetFunction(ActionID).op : "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? (CMSUser.GetFunction(ActionID).op_s == "Y" ? true : false) : false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
    }

    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

        if ("Y".Equals(e.NewValues["flag"]))
        {
            e.NewValues["fsrq"] = now;
        }
        else
        {
            e.NewValues["qxrq"] = now;
        }

        e.NewValues["founder"] = CMSUser.Current.ID;
        e.NewValues["fdate"] = now;
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = now;

        //e.NewValues["qsrq"] = Convert.ToDateTime(e.NewValues["qsrq"]).ToString("yyyy/MM/dd");
        //e.NewValues["zzrq"] = Convert.ToDateTime(e.NewValues["qsrq"]).ToString("yyyy/MM/dd");
    }
    protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

        //状态发生变化
        if (!e.NewValues["flag"].Equals(e.OldValues["flag"]))
        {
            if ("Y".Equals(e.NewValues["flag"]))
            {
                e.NewValues["fsrq"] = now;
            }
            else
            {
                e.NewValues["qxrq"] = now;
            }
        }

        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = now;

        //e.NewValues["qsrq"] = Convert.ToDateTime(e.NewValues["qsrq"]).ToString("yyyy/MM/dd");
        //e.NewValues["zzrq"] = Convert.ToDateTime(e.NewValues["qsrq"]).ToString("yyyy/MM/dd");
    }

    protected void gridView_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
    {
        if (!e.NewValues["flag"].Equals(e.OldValues["flag"]) || !e.NewValues["csbm"].Equals(e.OldValues["csbm"]))
        {
            if ("Y".Equals(e.NewValues["flag"]))
            {
                //修改供应商的生效合同编码
                using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
                {
                    DAL.css_csdj cs = db.css_csdj.FirstOrDefault(c => c.csbm == e.NewValues["csbm"].ToString());
                    if (cs != null)
                    {
                        cs.htbh = e.NewValues["htbm"].ToString();
                    }
                    db.SubmitChanges();
                }
            }
        }
    }

    protected void gridView_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
    {
        if ("Y".Equals(e.NewValues["flag"]))
        {
            //修改供应商的生效合同编码
            using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
            {
                DAL.css_csdj cs = db.css_csdj.FirstOrDefault(c => c.csbm == e.NewValues["csbm"].ToString());
                if (cs != null)
                {
                    cs.htbh = e.NewValues["htbm"].ToString();
                }
                db.SubmitChanges();
            }
        }
    }

    protected void gridView_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "htbm")
        {
            if (e.Value == null)
            {
                using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
                {
                    int number = db.css_contract.Count() + 1;
                    (e.Editor as ASPxTextBox).Text = WebUtility.GenerateCode(number.ToString(), 'H', 10);
                }
            }
        }

        if (e.Column.FieldName == "qsrq" || e.Column.FieldName == "zzrq")
        {
            if (gridView.IsNewRowEditing)
            {
                (e.Editor as ASPxTextBox).Text = DateTime.Now.ToString("yyyy/MM/dd");
            }
        }

    }

    [WebMethod]
    public static string[] GetFee(string fybm)
    {
        using (DAL.DataClassesDataContext context = new DAL.DataClassesDataContext())
        {
            string fylx = "";
            string fyje = "0";
            string fkfs = "";
            string kl = "0";
            DAL.css_fee fee = context.css_fee.FirstOrDefault(f => f.fybm == fybm);
            if (fee != null)
            {
                fylx = fee.fylx;
                fyje = fee.fyje.ToString();
                fkfs = fee.fkfs;
                kl = fee.kl.ToString();
            }
            return new string[] { fylx, fyje, fkfs, kl };
        }
    }

    #region 子表处理
    string htbm = string.Empty;

    protected void gridDetail_Load(object sender, EventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        Frame.Global.GlobalFunctions.SetGridView(grid, false);
        if (!string.IsNullOrEmpty(UserFunctions))
        {
            if (UserFunctions.Contains("I") || UserFunctions.Contains("U"))
            {
                Frame.Global.GlobalFunctions.SetGridView(grid, true);
            }
        }
        Frame.Global.GlobalFunctions.AddGridViewEvent(grid);
        htbm = grid.GetMasterRowKeyValue().ToString();
        grid.DataBind();
        grid.Styles.AlternatingRow.Enabled = DefaultBoolean.False;
    }

    protected void gridFy_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "jlxh")
        {
            if (e.Value == null)
            {
                using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
                {
                    int number = db.css_contract_fee.Where(o => o.htbm == htbm).Count() + 1;
                    (e.Editor as ASPxTextBox).Text = number.ToString();
                }
            }
        }
    }

    protected void FYMDSourceMain_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        e.KeyExpression = "jlxh";
        DAL.DataClassesDataContext Dcontext = new DAL.DataClassesDataContext();
        e.QueryableSource = Dcontext.css_contract_fee.Where(o => o.htbm == htbm);
    }

    protected void gridFy_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        e.NewValues["htbm"] = htbm;
    }

    protected void gridSp_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "jlxh")
        {
            if (e.Value == null)
            {
                using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
                {
                    int number = db.css_contract_sp.Where(o => o.htbm == htbm).Count() + 1;
                    (e.Editor as ASPxTextBox).Text = number.ToString();
                }
            }
        }
    }

    protected void SPMDSourceMain_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        e.KeyExpression = "jlxh";
        DAL.DataClassesDataContext Dcontext = new DAL.DataClassesDataContext();
        e.QueryableSource = Dcontext.css_contract_sp.Where(o => o.htbm == htbm);
    }

    protected void gridSp_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        e.NewValues["htbm"] = htbm;
    }
    #endregion
}